package pluginMain;

import java.util.Collection;

import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;

public class ItemSelectDialog implements Dialog, InventoryHolder, ListItemEventListener {
	private String dialogTitle = "Item Select Dialog";
	private Inventory dialogHolder = null;

	public ItemSelectDialog(String DialogTitle, Collection<? extends ItemStack> Items) {
		dialogTitle = DialogTitle;
		dialogHolder = DeliayGUI.Maininstance.getServer().createInventory(this, (int) (Items.size() / 9) + 1,
				dialogTitle);
	}

	public ItemSelectDialog(String DialogTitle, Inventory Items) {
		dialogTitle = DialogTitle;
		dialogHolder = Items;
		dialogHolder = DeliayGUI.Maininstance.getServer().createInventory(this, Items.getSize(), dialogTitle);
		dialogHolder.setContents(Items.getContents());

	}

	public ItemSelectDialog(String DialogName, String DialogTitle) {
		dialogTitle = DialogTitle;
		dialogHolder = DeliayGUI.Maininstance.getServer().createInventory(this, 27, dialogTitle);
	}

	public ItemSelectDialog(String DialogName, String DialogTitle, int DialogSize) {
		dialogTitle = DialogTitle;
		dialogHolder = DeliayGUI.Maininstance.getServer().createInventory(this, DialogSize, dialogTitle);
	}

	public ItemSelectDialog(String DialogName, String DialogTitle, InventoryType DialogSize) {
		dialogTitle = DialogTitle;
		dialogHolder = DeliayGUI.Maininstance.getServer().createInventory(this, DialogSize, dialogTitle);
	}
	
	@Override
	public Inventory getInventory() {
		return dialogHolder;
	}

	public void addItem(int slot, ItemStack item) {
		dialogHolder.setItem(slot, item);
	}

	public void removeItem(int slot) {
		dialogHolder.setItem(slot, null);
	}

	public void removeStack(ItemStack item) {
		dialogHolder.remove(item);
	}

	public void openDialog(Player who) {
		if (who.isOnline()) {
			who.openInventory(dialogHolder);
		}
	}

	@Override
	public void OpenDialog(Dialog dialog, Player who) {
		DeliayGUI.Maininstance.CONSOLE_LOGGER.info(who.getName() + " Opened");
	}

	@Override
	public void PlayerClickSlot(Player who, Dialog dialog, int slot) {
		DeliayGUI.Maininstance.CONSOLE_LOGGER.info(who.getName() + " Click Slot " + slot);
	}

	@Override
	public void PlayerClickItem(Player who, Dialog dialog, ItemStack item) {
		DeliayGUI.Maininstance.CONSOLE_LOGGER.info(who.getName() + " Click Item " + item.getItemMeta().getDisplayName());
	}

	@Override
	public void PlayerTakeItem(Player who, Dialog dialog, ItemStack item) {
		DeliayGUI.Maininstance.CONSOLE_LOGGER.info(who.getName() + " Take Item " + item.getItemMeta().getDisplayName());
	}

	@Override
	public void CloseDialog(Dialog dialog, Player who) {
		DeliayGUI.Maininstance.CONSOLE_LOGGER.info(who.getName() + " Closed");
	}
}
